﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using PMS.ENT;
using PMS.Utilities;
using System.Data;
using System.Data.SqlClient;
using Microsoft.Practices.EnterpriseLibrary.Data;
using System.Data.Common;

namespace PMS.DAL
{
    public class ProgressDAL
    {
        #region Common
        private static ProgressENT CreateProgressFromReader(IDataReader reader)
        {
            ProgressENT item = new ProgressENT();
            try
            {
                if (!reader.IsDBNull(reader.GetOrdinal(ProgressColumns.ProgressID.ToString()))) item.ProgressID = Utility.IntParse(reader[ProgressColumns.ProgressID.ToString()]);
                if (!reader.IsDBNull(reader.GetOrdinal(ProgressColumns.ProjectID.ToString()))) item.ProjectID = Utility.IntParse(reader[ProgressColumns.ProjectID.ToString()]);
                if (!reader.IsDBNull(reader.GetOrdinal(ProgressColumns.ProjectName.ToString()))) item.ProjectName = Utility.StringParse(reader[ProgressColumns.ProjectName.ToString()]);
                if (!reader.IsDBNull(reader.GetOrdinal(ProgressColumns.ScheduleID.ToString()))) item.ScheduleID = Utility.IntParse(reader[ProgressColumns.ScheduleID.ToString()]);
                if (!reader.IsDBNull(reader.GetOrdinal(ProgressColumns.ScheduleName.ToString()))) item.ScheduleName = Utility.StringParse(reader[ProgressColumns.ScheduleName.ToString()]);
                if (!reader.IsDBNull(reader.GetOrdinal(ProgressColumns.GroupID.ToString()))) item.GroupID = Utility.IntParse(reader[ProgressColumns.GroupID.ToString()]);
                if (!reader.IsDBNull(reader.GetOrdinal(ProgressColumns.GroupName.ToString()))) item.GroupName = Utility.StringParse(reader[ProgressColumns.GroupName.ToString()]);
                if (!reader.IsDBNull(reader.GetOrdinal(ProgressColumns.Progress.ToString()))) item.Progress = Utility.DecimalParse(reader[ProgressColumns.Progress.ToString()]);
                if (!reader.IsDBNull(reader.GetOrdinal(ProgressColumns.Mark.ToString()))) item.Mark = Utility.DecimalParse(reader[ProgressColumns.Mark.ToString()]);

             
            }
            catch
            {
                throw;
            }
            return item;
        }
        #endregion

        #region Get

        public static ProgressCollection GetProgressList(string role,int userID,int projectID,int semesterID, int scheduleID, int groupID, string orderBy, string orderDirection, int page, int pageSize, out int totalRecords)
        {
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand(Procedure.procProgressGetList.ToString());
                ProgressCollection listProgress = new ProgressCollection();
                database.AddInParameter(dbCommand, "@Role", DbType.String, role);
                database.AddInParameter(dbCommand, "@UserID", DbType.Int32, userID);
                database.AddInParameter(dbCommand, "@SemesterID", DbType.Int32, semesterID);
                database.AddInParameter(dbCommand, "@ProjectID", DbType.Int32, projectID);
                database.AddInParameter(dbCommand, "@ScheduleID", DbType.Int32, scheduleID);
                database.AddInParameter(dbCommand, "@GroupID", DbType.Int32, groupID);
                database.AddInParameter(dbCommand, "@OrderBy", DbType.AnsiString, orderBy);
                database.AddInParameter(dbCommand, "@OrderDirection", DbType.AnsiString, orderDirection);
                database.AddInParameter(dbCommand, "@Page", DbType.Int32, page);
                database.AddInParameter(dbCommand, "@PageSize", DbType.Int32, pageSize);
                database.AddOutParameter(dbCommand, "@TotalRecords", DbType.Int32, 0);

                using (IDataReader reader = database.ExecuteReader(dbCommand))
                {
                    while (reader.Read())
                    {
                        listProgress.Add(CreateProgressFromReader(reader));
                    }
                    reader.Close();
                }

                totalRecords = (int)database.GetParameterValue(dbCommand, "@TotalRecords");

                return listProgress;
            }
            catch (SqlException)
            {
                throw new DataAccessException("BLM_ERR_GetProgressList");
            }
        }

        #endregion
    }
}
